<template>
    <div>
        <van-nav-bar title="会员登录" left-text="返回" left-arrow @click-left="()=>{$router.push('/')}" />

        <van-form @submit="onSubmit">
            <div style="width: 100%;">  <img src="../img/QQ浏览器截图20250728095105.png" alt="" style="margin-left: 150px;"></div>
            
     

            <van-cell-group inset>
                <van-field v-model="username" name="username" label="账号" placeholder="请输入账号(必填)"
                    :rules="[{ required: true, message: '请输入账号' }]" />
                <van-field v-model="password" type="password" name="password" label="密码" placeholder="请输入密码(必填)"
                    :rules="[{ required: true, message: '请输入密码' }]" />

            </van-cell-group>

            <div style="margin: 16px;">
                <van-button block type="primary" native-type="submit" style="border: none;background-color: crimson;">
                    登录
                </van-button>
            </div>
        </van-form>
        <div style="margin: 16px;">
            <van-button block style="border: none;" @click="zc">
                免费注册
            </van-button>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue';
const username = ref('');
const password = ref('');

import { loginApi } from '@/api/api';
import { showSuccessToast, showFailToast } from 'vant';
import { useRouter } from 'vue-router';
const router = useRouter()
const onSubmit = (values) => {
    console.log('submit', values);
    loginApi(username.value, password.value).then(res => {
        console.log(res);
        if (res.data.code == 0) {
            showSuccessToast('登录成功');
            router.push('/home')
            localStorage.setItem('token', res.data.data.token)
            localStorage.setItem('username', res.data.data.username)
        } else {
            showFailToast('登录失败');
        }
    })
};
const zc = () => {
    router.push('/')
}
</script>

<style lang="scss" scoped></style>